<script>
	import { mapState, mapActions } from 'pinia'
	import { useUserinfo } from '@/store/userInfo.js'
	import request from '@/utils/request.js'
	export default {
		onLaunch: function(options) {
			this.reWriteUniFuns()
			this.login(options)
			this.getPublicInfo()
		},
		onShow: function() {
			if(!this.userinfo.isAuth || this.userinfo.isAuth === 0) {
				this.getUserinfo()
			}
		},
		onHide: function() {
		},
		computed: {
			...mapState(useUserinfo,['userinfo','token','requestList','hasLogin','isAdminChecker'])
		},
		methods: {
			getPublicInfo() {
				request({
					url:'api/login/getVersion',
					method:'post',
					needLogin: false,
					data: {
						version: uni.getSystemInfoSync().appVersion
					}
				}).then(res => {
					console.log(res,'publicInfo')
					this.setPublicInfo(res)
				})
			},
			reWriteUniFuns() {
				const navigateTo = uni.navigateTo;
				const switchTab = uni.switchTab;
				const redirectTo = uni.redirectTo;
				uni.navigateTo = (parmas) => {
					if(this.userinfo.isAuth === 0 && !this.isAdminChecker) {
						redirectTo({
							url: '/pages/auth/auth'
						})
					}else {
						navigateTo(parmas)
					}
				}
				uni.switchTab = (parmas) => {
					if(this.userinfo.isAuth === 0 && !this.isAdminChecker) {
						redirectTo({
							url: '/pages/auth/auth'
						})
					}else {
						switchTab(parmas)
					}
				}
				uni.redirectTo = (parmas) => {
					if(this.userinfo.isAuth === 0 && !this.isAdminChecker) {
						redirectTo({
							url: '/pages/auth/auth'
						})
					}else {
						redirectTo(parmas)
					}
				}
			},
			...mapActions(useUserinfo, ['setUserinfo','getUserinfo','setToken','setRequestList','setLogin','setPublicInfo']),
			getUserinfo(from) {
				request({
					url: 'api/login/autoLogin',
					needLogin: true
				}).then(res => {
					this.setToken(res.token)
					this.setUserinfo(res)
					if(res.isChecker) {
						uni.redirectTo({
							url: '/pages/mine/checkUser'
						})
					}else {
						if(res.isAuth === 1) {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}else {
							if(from === 'auth') {
							// 从提交信息页面  /pages/auth/auth 来刷新时候的逻辑
								uni.showToast({
									title: '请耐心等待审核',
									icon:'none'
								})
							}else {
								uni.redirectTo({
									url: '/pages/auth/auth'
								})
							}
						}
					}
				}).catch(err => {
				})
			},
			login(options) {
				const asyncLogin = () => {
					this.getUserinfo()
					if (this.requestList.length > 0) {
						// 如果有需要登录的接口在未登录之前执行了，则需要执行以下代码
						this.requestList.forEach(item => {
							item.request(item.data).then(res => {
								item.resolve(res)
							}).catch(err => {
								item.reject(err)
							})
						})
						this.setRequestList([]);
					} else {
						// 正常流程

					}
				}

				const loginFun = (res, extra_params) => {
					console.log(1111)
					request({
						url: 'api/login/loginByCode',
						data: {
							code: res.code,
						},
					}).then(res => {
						this.setToken(res.token)
						console.log(this.token,res.token)
						this.setLogin(true)
						asyncLogin()
					}).catch(err => {
					})
				}
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin', //使用微信登录
					success: res => {
						console.log(res, 'ress')
						loginFun(res)
					}
				})
				// #endif
				// 公众号登录
				// #ifdef H5
				this.setToken('cc74253dafd1e634b87b689a943f57f8')
				this.setToken('eb56ce3d78f195e9b308252af0654f3b')
				// 1524b179db2640350d303603dec00e03
				this.setLogin(true)
				asyncLogin()
				// #endif
			},
		},
		globalData: {
			// 统一处理消息订阅
			subscribe: (tmplIds = config.tmplIds) => {
				return new Promise((resolve, reject) => {
					uni.requestSubscribeMessage({
						tmplIds,
						success(res) {
							const acceptIds = tmplIds.filter(item => res[item] === 'accept');
							if (acceptIds.length > 0) {
								uni.showToast({
									icon: 'none',
									title: '订阅成功！'
								})
								request({
									url: 'mobile/user/user_subscribe',
									data: {
										tmpl_id: acceptIds.join(',')
									}
								}).then(res => {})
								resolve()
							} else {
								uni.showToast({
									icon: 'none',
									title: '订阅失败！'
								})
								reject()
							}
						},
						fail(err) {
							console.log(err, 'err')
							uni.showToast({
								icon: 'none',
								title: '订阅失败！'
							})
							reject()
						}
					})
				})
			},
		}

	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "uview-plus/index.scss";
</style>